//
//  RuiQueViewModel.m
//  Ruique
//
//  Created by xiute on 2018/10/28.
//  Copyright © 2018 cuijin. All rights reserved.
//

#import "RuiQueViewModel.h"
#import "RuiQueTableViewCell.h"
#import "RuiQueModel.h"
#import "../Service/NetService.h"

static NSString * const kRuiQueCellIndentifier = @"kRuiQueCellIndentifier";

@interface RuiQueViewModel ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray<RuiQueModel *> *allDataSource;

@property (nonatomic, strong) NSArray<RuiQueModel *> *displayDataSource;

@end

@implementation RuiQueViewModel

- (void)registTableView:(UITableView *)tableView {
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView registerNib:[UINib nibWithNibName:nibName bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kRuiQueCellIndentifier];
}


- (void)refreshData:(void (^)(void))complete {
    [[NetService shareInstance]getUrl:@"raw/8wyuMzqR" param:nil Complete:^(BOOL success, NSError * _Nullable error, NSURLSessionDataTask *dataTask, id  _Nullable response) {
        if (success &&
            [response isKindOfClass:[NSDictionary class]] &&
            [response[@"data"] isKindOfClass:[NSArray class]]) {
            [self handlenetData:response];
        }
        if (!success) {
            //处理网络结果错误 比如可以展示无网页面 弹出dialog 加上一些打点记录之类的
        }
        if (complete) {
            complete();
        }
    }];
}

- (void)handlenetData:(NSDictionary *)response {
    [self.allDataSource removeAllObjects];
    NSArray *dataArray = response[@"data"];
    for (NSDictionary *dataDic in dataArray) {
        NSError *error;
        RuiQueModel *model = [[RuiQueModel alloc]initWithDictionary:dataDic error:&error];
        if (error) {
            continue;
        }
        [self.allDataSource addObject:model];
    }
    self.displayDataSource = [NSArray arrayWithArray:self.allDataSource];
    [self handleSearchValue];
}

- (void)handleSearchValue {
    NSMutableArray *tempArray = [NSMutableArray array];
    [self.allDataSource enumerateObjectsUsingBlock:^(RuiQueModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (![tempArray containsObject:obj.quoteAssetUnit] &&
            obj.quoteAssetUnit.length > 0) {
            [tempArray addObject:obj.quoteAssetUnit];
        }
    }];
    _searchValue = [NSArray arrayWithArray:tempArray];
}

- (void)searchUnit:(NSString *)unit {
    if (unit.length > 0) {
        self.displayDataSource = [self.allDataSource filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(RuiQueModel *  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
            if ([evaluatedObject.quoteAssetUnit isEqualToString:unit]) {
                return YES;
            }else {
                return NO;
            }
        }]];
    }
}


#pragma mark -tableview delegate and datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.displayDataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RuiQueTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kRuiQueCellIndentifier forIndexPath:indexPath];
    [cell configData:self.displayDataSource[indexPath.row]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}



#pragma mark -properties
- (NSMutableArray<RuiQueModel *> *)allDataSource {
    if (!_allDataSource) {
        self.allDataSource = [NSMutableArray array];
    }
    return _allDataSource;
}

@end
